package com.example.demo_test.Service.Imp;

import com.example.demo_test.DTO.CreateGroupDto;
import com.example.demo_test.Dao.GroupDao;
import com.example.demo_test.Dao.MessageDao;
import com.example.demo_test.Dao.PersonDao;
import com.example.demo_test.Entity.Group;
import com.example.demo_test.Entity.ReqRecord;
import com.example.demo_test.Entity.Result;
import com.example.demo_test.Service.GroupChatService;
import com.example.demo_test.Service.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author zhanghong
 * @date 2021/6/8  0008 15:02
 */
@Service()
public class GroupChatServiceImp implements GroupChatService {
    @Autowired
    private GroupDao groupDao;
    @Autowired
    private PersonDao personDao;
    @Autowired
    private MessageDao messageDao;
    @Override
    public List<Group> getGroup(String userId) {
        List<Group> groupList=groupDao.getGroup(userId);
        for (Group group:groupList) {
            group.setMessages( messageDao.getGroupMessageList( group.getEid() ) );
            group.setUserList( personDao.getGroupUsers( group.getEid() ) );
        }
        return groupList;
    }

    @Override
    public Result createGroup(CreateGroupDto createGroupDto) {
        Result result=new Result();
        try {
            List<String> userList = Arrays.asList( createGroupDto.getUsers() );
            groupDao.createGroup( createGroupDto );

            int eid=createGroupDto.getEid();
            groupDao.joinGroup( createGroupDto.getLord(),eid,"true" );
            for (String user:userList
                 ) {
                ReqRecord reqRecord=new ReqRecord();
                reqRecord.setToUser( user );
                reqRecord.setFromUser( createGroupDto.getLord() );
                reqRecord.setOperate( "groupChat" );
                reqRecord.setStatus( "待同意" );
                reqRecord.setContent( "邀请你进入"+createGroupDto.getCreateGroupName() );
                reqRecord.setGroupId( eid );
                reqRecord.setDate( new Date() );

                personDao.addReqRecord( reqRecord );
                WebSocketService.pushReqRecord( reqRecord );
//                groupDao.joinGroup( user,eid,"false" );
            }
            result.setCode( 200 );
            result.setMessage( "创建群聊成功" );
        }catch (Exception e){
            e.printStackTrace();
            result.setCode( 400 );
            result.setMessage( "创建群聊失败" );
        }
        return result;
    }

    @Override
    public Result inviteFriends(String[] userIdList, int groupId, String currentUserId,String groupName) {

        try {
            for (String user : userIdList
            ) {
                ReqRecord reqRecord = new ReqRecord();
                reqRecord.setToUser( user );
                reqRecord.setFromUser( currentUserId );
                reqRecord.setOperate( "groupChat" );
                reqRecord.setStatus( "待同意" );
                reqRecord.setContent(  "邀请你进入" + groupName );
                reqRecord.setGroupId( groupId );
                reqRecord.setDate( new Date() );
                personDao.addReqRecord( reqRecord );
                WebSocketService.pushReqRecord( reqRecord );
//                groupDao.joinGroup( user,eid,"false" );
            }
            return new Result(200,"邀请成功，等待好友确认","");
        }catch (Exception e){
            return new Result(400,"网络繁忙，请稍后再试","");
        }
    }

    @Override
    public Result exitGroupChat(String userId, String groupId) {
        try {
            groupDao.exitGroupChat( userId, groupId );
            return new Result(200,"退出成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙,退出失败","");
        }
    }

    @Override
    public Result deleteGroupChat(String groupId) {
        try {
            groupDao.deleteGroupChat( groupId );
            return new Result(200,"删除成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙,删除失败","");
        }
    }
}
